package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.math.BigInteger;
import java.util.LinkedList;

@Cmd(code = Protocol.C2_COMMON_ACTIVITY_LAST_RANK, desc = "游戏最新排名")
public class CrossCommonActivityLastRankCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityRankProto.CrossActivityRankChangeReqMsg reqMsg = CrossActivityRankProto.CrossActivityRankChangeReqMsg.parseFrom(packet.getBytes());
        ActivityRankMgr.changeActivityNewRank(reqMsg.getActivityId(), reqMsg.getConditionType(), null);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(reqMsg.getActivityId(), reqMsg.getConditionType());
        int newRank = -1;
        if (rankListModel != null) {
            LinkedList<CrossUserRank> otherRankList = rankListModel.getRankList();
            CrossUserRank otherUserRank = new CrossUserRank();
            otherUserRank.setType(reqMsg.getActivityId());
            otherUserRank.setUserId(packet.getPlayerId());
            otherUserRank.setBigRankType(reqMsg.getConditionType());
            otherUserRank.setValue(BigInteger.valueOf(reqMsg.getScore()));
            otherUserRank.setParam("");
            rankListModel.otherRankChange(otherUserRank, otherRankList);
            newRank = rankListModel.getOtherRank(packet.getPlayerId(), serverId, otherRankList);
        }
        CrossActivityRankProto.CrossActivityRankChangeRespMsg.Builder msg = CrossActivityRankProto.CrossActivityRankChangeRespMsg.newBuilder();
        msg.setActivityId(msg.getActivityId());
        msg.setConditionType(msg.getConditionType());
        msg.setRank(newRank);
        YanQuMessage message = YanQuMessageUtils.buildMessage(reqMsg.getReBackCode(), msg);
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), message);
    }

    @Override
    public void sendData() {

    }
}
